package com.ms.oschina.fragment;

import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;

import com.ms.oschina.R;
import com.ms.oschina.base.BaseFragment;
import com.ms.oschina.emoji.MyOnExpressionSelectListener;
import com.ms.oschina.util.UiUtils;

public class ShotDetailBottomFragment extends BaseFragment implements
		OnClickListener {

	private EditText et;

	private ImageButton ibShotdetailbottomGallery;
	private ImageButton ibShotdetailbottomAt;
	private ImageButton ibShotdetailbottomTopic;
	private ImageButton ibShotdetailbottomEmoji;

	private ExpressionFragment expressionFragment;

	private FragmentTransaction beginTransaction;

	private OnClickListener onSelectorPhotolistener;

	private void findViews(View view) {
		ibShotdetailbottomGallery = (ImageButton) view
				.findViewById(R.id.ib_shotdetailbottom_gallery);
		ibShotdetailbottomAt = (ImageButton) view
				.findViewById(R.id.ib_shotdetailbottom_at);
		ibShotdetailbottomTopic = (ImageButton) view
				.findViewById(R.id.ib_shotdetailbottom_topic);
		ibShotdetailbottomEmoji = (ImageButton) view
				.findViewById(R.id.ib_shotdetailbottom_emoji);
	}

	/**
	 * @param et
	 *            当表情被点击时 ： 添加到哪个编辑框中
	 */
	public ShotDetailBottomFragment() {
	}

	public void setAddEmjoiEditText(EditText et) {
		this.et = et;
	}

	@Override
	public int getLayoutId() {
		return R.layout.fragment_shotdetailbottom;
	}

	@Override
	public void initView(View view) {
		findViews(view);
	}

	@Override
	protected void initData() {
		expressionFragment = getFragment();
		FragmentTransaction beginTransaction = getFragManager()
				.beginTransaction();
		beginTransaction.replace(R.id.fl, expressionFragment);
		beginTransaction.hide(expressionFragment);
		beginTransaction.commit();

		ibShotdetailbottomGallery.setOnClickListener(onSelectorPhotolistener);
		ibShotdetailbottomAt.setOnClickListener(this);
		ibShotdetailbottomTopic.setOnClickListener(this);
		ibShotdetailbottomEmoji.setOnClickListener(this);
	}

	public FragmentManager getFragManager() {
		FragmentActivity fa = (FragmentActivity) getActivity();
		return fa.getSupportFragmentManager();
	}

	public void onSelectorPhotoListener(OnClickListener listener) {
		onSelectorPhotolistener = listener;
	}

	/**
	 * 打开表情
	 */
	private void openExpression() {
		// 切换笑脸颜色为绿色
		ibShotdetailbottomEmoji.setImageResource(R.drawable.btn_emoji_pressed);

		beginTransaction = getFragManager().beginTransaction();
		beginTransaction.show(expressionFragment);
		beginTransaction.commit();
	}

	/**
	 * 关闭表情
	 */
	private void closeExpression() {
		ibShotdetailbottomEmoji.setImageResource(R.drawable.btn_emoji_normal);

		beginTransaction = getFragManager().beginTransaction();
		beginTransaction.hide(expressionFragment);
		beginTransaction.commit();
	}

	private Boolean isExpressionColse = true;

	@Override
	public void onClick(View v) {
		if (v == ibShotdetailbottomAt) {
		} else if (v == ibShotdetailbottomTopic) {
		} else if (v == ibShotdetailbottomEmoji) {
			if (isExpressionColse) {
				openExpression();
			} else {
				closeExpression();
			}
			isExpressionColse = !isExpressionColse;
		}
	}

	public ExpressionFragment getFragment() {
		MyOnExpressionSelectListener listener = new MyOnExpressionSelectListener(
				et);
		ExpressionFragment expressionFragment = new ExpressionFragment(listener);
		expressionFragment.setDelEditText(et);
		return expressionFragment;
	}

	public boolean autoLoadData() {
		return true;
	}

}
